﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using BitAuto.EP.CRMLogSystem.Entity;

namespace BitAuto.EP.CRMLogSystem.DAL
{
    public static class InputDBDAL
    {
        public static void BulkToDB(LogTableInfo info)
        {
            SqlConnection sqlConn = new SqlConnection(info.ConnectionString);
            SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConn);
            bulkCopy.DestinationTableName = info.TableName;
            bulkCopy.BatchSize =  info.Table.Rows.Count;
            foreach (var item in info.Fileds)
            {
                bulkCopy.ColumnMappings.Add(item.FieldName, item.FieldName);
            }
            
            try
            {
                sqlConn.Open();
                bulkCopy.WriteToServer(info.Table);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                sqlConn.Close();
                if (bulkCopy != null)
                    bulkCopy.Close();
            }
        }  
    }
}
